﻿using System.Linq;
using System.Web.Mvc;
using SportsStore.Domain.Abstract;
using SportsStore.Domain.Entities;
using SportsStore.WebUI.Models;

namespace SportsStore.WebUI.Controllers
{
    public class CartController : Controller
    {
        private readonly IProductsRepository _repository;
        private readonly IOrderProcessor _orderProcessor;

        public CartController(IProductsRepository repo, IOrderProcessor proc = null)
        {
            _repository = repo;
            _orderProcessor = proc;
        }

        #region Summary and Checkout
        public ViewResult Index(Cart cart, string returnUrl)
        {
            return View(
                new CartIndexViewModel
                    {
                        Cart = cart,
                        ReturnUrl = returnUrl
                    });
        }

        public ViewResult Summary(Cart cart)
        {
            return View(cart);
        }

        public ViewResult Checkout()
        {
            return View(new ShippingDetails());
        }

        [HttpPost]
        public ViewResult Checkout(Cart cart, ShippingDetails shippingDetails)
        {
            if (!cart.Lines.Any())
            {
                ModelState.AddModelError("", "Sorry, your cart is empty");
            }

            if (ModelState.IsValid)
            {
                _orderProcessor.ProcessOrder(cart, shippingDetails);
                cart.Clear();
                return View("Completed");
            }
            else
            {
                return View(shippingDetails);
            }
        }

        #endregion

        #region Item Management
        public RedirectToRouteResult AddToCart(Cart cart, int productId, string returnUrl)
        {
            var product = GetSpecificProduct(productId);

            if (product != null)
            {
                cart.AddItem(product, 1);
            }

            return RedirectToAction("Index", new {returnUrl});
        }

        public RedirectToRouteResult IncreaseItem(Cart cart, int productId, string returnUrl)
        {
            var product = GetSpecificProduct(productId);
            if (product != null)
            {
                cart.IncreaseItem(product, 1);
            }

            return RedirectToAction("Index", new {returnUrl});
        }

        public RedirectToRouteResult DecreaseItem(Cart cart, int productId, string returnUrl)
        {
            var product = GetSpecificProduct(productId);
            if (product != null)
            {
                cart.DecreaseItem(product, 1);
            }

            return RedirectToAction("Index", new {returnUrl});
        }

        public RedirectToRouteResult RemoveFromCart(Cart cart, int productId, string returnUrl)
        {
            var product = GetSpecificProduct(productId);
            if (product != null)
            {
                cart.RemoveLine(product);
            }

            return RedirectToAction("Index", new {returnUrl});
        }
        #endregion 

        private Product GetSpecificProduct(int productId)
        {
            return _repository
                .Products
                .FirstOrDefault(x => x.ProductId == productId);
        }
    }
}
